<template>
  <div class="form">
    <div class="title">生成计划</div>
    <el-form ref="form" :disabled="seeFlag" :model="form" label-width="120px" size="small" inline label-suffix="："
      :rules="rules">
      <el-form-item label="申请人" prop="">
        <el-input v-model="form.createUser" placeholder="" clearable disabled style="width:150px"></el-input>
      </el-form-item>
      <!-- <el-form-item label="作业码头"
                    prop="termName">
        <el-input v-model="form.termName"
                  placeholder=""
                  clearable
                  disabled
                  style="width:150px"></el-input>
      </el-form-item>
      <el-form-item label="码头作业区"
                    prop="workZone">
        <el-input v-model="form.workZone"
                  placeholder=""
                  clearable
                  disabled
                  style="width:150px"></el-input>
      </el-form-item> -->
      <el-form-item label="作业码头" v-if="showTKFZ" :prop="termDis ? '':'termCode'">
        <el-select clearable style="width: 150px" size="small" :disabled="termDis" v-model="form.termCode"
          placeholder="请选择作业码头">
          <el-option :label="item.cnName" :value="item.termCode" v-for="item,index in termList"
            :key="index"></el-option>
        </el-select>
      </el-form-item>
      <!-- @change="getVel" <el-form-item label="码头作业区"
                    prop="workzone">
          <el-select v-model="form.workzone"
                    :disabled="!form.termCode"
                     placeholder="请选择"
                     clearable
                     filterable
                     style="width:150px">
              <el-option v-for="item in dataList.workZone"
                     :key="item.id"
                     :label="item.cnName"
                     :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="showFCZYQ"
                    label="返厂作业区"
                    prop="returnWorkzone">
        <el-select v-model="form.returnWorkzone"
                   :disabled="!form.termCode"
                   placeholder="请选择"
                   clearable
                   filterable
                   style="width:150px">
          <el-option v-for="item in dataList.workZone"
                     :key="item.id"
                     :label="item.cnName"
                     :value="item.id"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="计划类型" v-if="showTKFZ" :prop="termDis?'':'planTagName'">
        <el-input v-model="form.planTagName" placeholder="" clearable disabled style="width:150px"></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="opsttm">
        <el-date-picker type="datetime" @change="setVal" placeholder="选择日期" clearable value-format="yyyy-MM-dd HH:mm:ss"
          v-model="form.opsttm" style="width:150px"></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" :prop="termDis?'':'opedtm'">
        <el-date-picker type="datetime" placeholder="选择日期" disabled clearable value-format="yyyy-MM-dd HH:mm:ss"
          v-model="form.opedtm" style="width:150px"></el-date-picker>
      </el-form-item>
      <el-form-item label="结算方式" :prop="termDis?'':'payMode'">
        <el-select v-model="form.payMode" disabled placeholder="请选择" clearable filterable style="width:150px">
          <el-option v-for="item in dataList.SETTLEMODE" :key="item.lowValue" :label="item.meaning"
            :value="item.lowValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="付款单位" prop="payer">
        <el-select v-model="form.payer" placeholder="请选择" clearable filterable style="width:150px">
          <el-option v-for="item in dataList.cstCoperListtomer" :key="item.customerCode" :label="item.name"
            :value="item.customerCode"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="货主单位" prop="consignor">
        <el-select v-model="form.consignor" placeholder="请选择" clearable filterable style="width:150px">
          <el-option v-for="item in dataList.consignor" :key="item.customerCode" :label="item.name"
            :value="item.customerCode"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车队单位" prop="cstTrnscomCd">
        <el-select v-model="form.cstTrnscomCd" placeholder="请选择" clearable filterable style="width:150px">
          <el-option v-for="item in dataList.cstTrnscomCd" :key="item.customerCode" :label="item.name"
            :value="item.customerCode"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="showFPTT" label="发票抬头" prop="title">
        <el-input v-model="form.title" placeholder="请输入" clearable filterable style="width:150px">
        </el-input>
      </el-form-item>
      <el-form-item label="返场标记" :prop="termDis?'':'title'" v-if="showFCBJ">
        <el-checkbox disabled v-model="form.checked" checked></el-checkbox>
      </el-form-item>
      <el-form-item label="备注" prop="remark" style="display:block">
        <el-input type="textarea" placeholder="请输入船期备注" clearable v-model="form.remark" style="width:710px"></el-input>
      </el-form-item>
      <!-- v-if="showUpload" -->
      <el-form-item label="附件" prop="planAttachmentList" style="display:block">
        <uploadImg @set="getFile" ref="myUpload"></uploadImg>
      </el-form-item>
    </el-form>
    <div class="btn">
      <el-button type="primary" v-if="!seeFlag" :loading="loading" @click="getDate" class="search">生成计划
      </el-button>
    </div>
  </div>
</template>

<script>
import uploadImg from '@/components/uploadImg.vue'
import moment from 'moment'
export default {
  name: 'addPlanForm',
  props: {
    showUpload: {
      default: true,
    },
    showFCZYQ: {
      default: false,
    },
    showFPTT: {
      default: true,
    },
    showFCBJ: {
      default: false,
    },
    Tag: '',
    showTKFZ:{
      default:true,
    },
    seeFlag:{
      default:false
    },
    termDis:{
      default:true,
    }
  },
  watch: {
    Tag() {
      let that = this
      this.$nextTick(() => {
        that.palnTagList.forEach((item) => {
          // console.log(999)
          // console.log(item)
          if (that.Tag === item.key) {
            that.form.planTag = item.key
            that.form.planTagName = item.value
            if(['提空返重（不指定箱号）','提空返重（指定箱号）','进空箱','进重箱','提空箱（不指定箱号）','装箱（不指定箱号）'].includes(that.form.planTagName)){
              this.form.opedtm='9998-12-31 23:59:59'
              this.$forceUpdate()
            }
          }
        })
      })
    },
  },
  components: { uploadImg },
  data() {
    return {
      loading:false,
      palnTagList: [
        { key: 'PFRF', value: '提重返重' },
        { key: 'PFRE', value: '提重返空' },
        { key: 'PERF_WC', value: '提空返重（指定箱号）' },
        { key: 'PERF_NC', value: '提空返重（不指定箱号）' },
        { key: 'PE_WC', value: '提空箱（指定箱号）' },
        { key: 'PE_NC', value: '提空箱（不指定箱号）' },
        { key: 'PF', value: '提重箱' },
        { key: 'IF', value: '进重箱' },
        { key: 'IE', value: '进空箱' },
        { key: 'BOX', value: '装箱' },
        { key: 'UNBOX', value: '拆箱' },
      ],
      form: {
        payMode:'ZD'
      },
      oriForm: {
        payMode:'ZD'
      },
      rules: {
        termCode: [
          { required: true, message: '请选择作业码头', trigger: 'blur' },
        ],
        planTagName: [
          { required: true, message: '请输入计划类型', trigger: 'blur' },
        ],
        // returnWorkzone: [
        //   { required: true, message: '请选择返厂作业区', trigger: 'change' },
        // ],
        opsttm: [
          { required: true, message: '请选择开始时间', trigger: 'change' },
        ],
        opedtm: [
          { required: true, message: '请选择结束时间', trigger: 'blur' },
        ],
        payMode: [
          { required: true, message: '请选择结算方式', trigger: 'change' },
        ],
        payer: [
          { required: true, message: '请选择付款单位', trigger: 'change' },
        ],
        cstTrnscomCd: [
          { required: true, message: '请选择车队单位', trigger: 'change' },
        ],
        planAttachmentList: [
          { required: true, message: '请上传附件', trigger: 'change' },
        ],
      },
      dataList: {},
      payMode: [
        { value: '现金', key: 'XJ' },
        { value: '月结', key: 'ZD' },
      ],
      termList:[],
    }
  },
  methods: {
    setVal(){
      if(''!==this.form.opsttm){
        console.log(9999)
        console.log
        if(['提重返空','提重箱','提空箱（指定箱号）','拆箱','装箱（指定箱号）'].includes((this.form.planTagName))){
            console.log(this.form.opsttm)
            this.form.opedtm=moment(this.form.opsttm).add(7,'days').subtract(1,'seconds').format('YYYY-MM-DD HH:mm:ss')
            this.$forceUpdate()
            //.subtract(1,'seconds')
        }
      }
    },
    getWork() {
      this.$api
        .opaAreaCode({
          termCode: this.form.termCode,
        })
        .then((res) => {
          this.dataList.workZone = res.data.data
          this.$forceUpdate()
        })
        .catch()
    },
    set(val) {
      if ('HX' === val) {
        this.form.termCode = 'HX'
        // this.form.termName = '河西码头'
        // this.form.termCode = 'HX'
        // this.form.workZone = '河西港'
        this.$forceUpdate()
        //console.log(this.$refs.addPlan.form)
      }
    },
    getDate() {
      this.$emit('add')
    },
    getFile(res) {
      // console.log(666)
      // console.log
      // this.form.planAttachmentList = res
      this.$set(this.form,'planAttachmentList',res)
    },
    setFile() {
      let par = {}
      this.$api.term().then(res => {
					this.termList = res.data.data
			})
      this.$api
        .refCode({
          domain: 'SETTLEMODE',
        })
        .then((res) => {
          this.dataList.SETTLEMODE = res.data.data
          this.$forceUpdate()
        })
        .catch()

      this.$api
        .customerByType({
          tenantTypeName: '货主',
        })
        .then((res) => {
          this.dataList.consignor = res.data.data
          this.$forceUpdate()
        })
        .catch()
      this.$api
        .customerByType({
          tenantTypeName: '运输公司',
        })
        .then((res) => {
          this.dataList.cstTrnscomCd = res.data.data
          this.$forceUpdate()
        })
        .catch()
      this.$api
        .customer()
        .then((res) => {
          this.dataList.cstCoperListtomer = res.data.data
          this.$forceUpdate()
        })
        .catch()
      let info = localStorage.getItem('userInfo')
      console.log(info)
      if (undefined !== info && !this.form.cstCstmcd) {
        this.form.cstCstmcd = JSON.parse(info).customerCode
        this.form.createUser = JSON.parse(info).name
      }
      this.oriForm = JSON.parse(JSON.stringify(this.form))
    },
  },

  computed: {},
  created(){
    this.setFile()
  },
  mounted() {
    this.oriForm = JSON.parse(JSON.stringify(this.form))
    //this.getDate()
  },
}
</script>
<style lang='less' scoped>
.form {
  margin-top: 25px;
  background-color: #fff;
  padding: 20px;

  .title {
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #333333;
    line-height: 16px;
    border-left: 5px solid #0f47b8;
    line-height: 16px;
    padding-left: 8px;
    margin-bottom: 25px;
  }
}

.btn {
  text-align: center;

  .search {
    background: #0f47b8;
    height: 30px;
    padding: 5px 10px;
  }
}

/deep/ .el-upload--picture-card {
  width: 80px;
  height: 80px;
  line-height: 80px;
}
</style>